package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.JxcRole;
import com.hhs.storage.bean.Result;
import com.hhs.storage.bean.User;
import com.hhs.storage.service.JxcRoleUserService;
import com.hhs.storage.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@RestController
@RequestMapping("do/user")
@Slf4j
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public Map<String, Object> getAll(InputParam inputParam) {
        log.info("查询用户分页信息");
        EntityWrapper ew = new EntityWrapper<User>();
        if(StringUtils.isNotEmpty(inputParam.getName())){
            ew.like("name_",inputParam.getName());
        }
        ew.orderBy("create_time_");
        Page<JxcRole> result = userService.selectPage(new Page<User>(inputParam.getPage(), inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }

    @RequestMapping(value = "getAllChild", method = RequestMethod.GET)
    public Map<String, Object> getAllChild(InputParam inputParam) {
        log.info("查询推荐用户分页信息");
        Page<User> result = userService.findPageChild(inputParam);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }

    @RequestMapping(value = "getSubIds")
    public Map<String, Object> getSubIds(InputParam inputParam) {
        log.info("下级用户的id");
        String result = userService.findIds(inputParam);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("subids",result);
        return map;
    }

    @RequestMapping(value = "getOneSubCount")
    public Map<String, Object> getOneSubCount(InputParam inputParam) {
        log.info("直接下级用户的个数");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        int maxcount=userService.findmaxcount(user.getId());
        int result = userService.findonelever(inputParam);
        int rm=maxcount-result;
        if(maxcount!=-1) {
            if(rm<=0){
                return Result.failJsonResp("超过您最大人数的限制了!请和管理员联系");
            }else{
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("code", 1);
                map.put("data", rm);
                return map;
            }
        }
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "edit")
    public Map<String, Object> saveOrUpdate(User user,String roleStr){
        log.info("新增或插入用户信息");
        if(StringUtils.isEmpty(user.getAccount())){
            return Result.failJsonResp("账号不能为空");
        }
        return userService.saveOrUpdate(user,roleStr);
    }

    @RequestMapping(value = "editChild")
    public Map<String, Object> editChildsaveOrUpdate(User user,String roleStr){
        log.info("新增或插入下级用户信息");
        if(StringUtils.isEmpty(user.getAccount())){
            return Result.failJsonResp("账号不能为空");
        }
        User userModel = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
        user.setPid(userModel.getId());
        return userService.saveOrUpdate(user,roleStr);
    }

    @RequestMapping(value = "modifypass")
    public Map<String, Object> modifypass(String oldpass,String newpass){
        log.info("修改密码");
        Map<String, Object> mapr=userService.modifypass(oldpass,newpass);
        return mapr;
    }

    @RequestMapping(value = "editRole")
    public Map<String, Object> editRole(String userId,@RequestParam("roleArr[]") String[] roleArr){
        log.info("新增或插入用户信息");
        if(StringUtils.isEmpty(userId)){
            return Result.failJsonResp("账号不能为空");
        }
        return userService.editRole(userId,roleArr);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除用户");
        userService.dele(id);
        return Result.sucJsonResp(null);

    }

}

